package com.juma.cartridge.rlock;


public class LockNode {

	private String key;
	private String lockId;
	private long expireTime;
	private int lockSeconds;
	
	public LockNode() {}
	
	public LockNode(String key,String lockId,long expireTime,int lockSeconds) {
		this.key = key;
		this.lockId = lockId;
		this.expireTime = expireTime;
		this.lockSeconds = lockSeconds;
	}
	
	public void setKey(String key) {
		this.key = key;
	}

	public String getKey() {
		return key;
	}

	public String getLockId() {
		return lockId;
	}
	public long getExpireTime() {
		return expireTime;
	}
	public int getLockSeconds() {
		return lockSeconds;
	}
	public void setLockId(String lockId) {
		this.lockId = lockId;
	}
	public void setExpireTime(long expireTime) {
		this.expireTime = expireTime;
	}
	public void setLockSeconds(int lockSeconds) {
		this.lockSeconds = lockSeconds;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof LockNode) {
			LockNode sv = (LockNode)obj;
			return this.lockId.equals(sv.getLockId());
		}
		return false;
	}
	
	
	public String toString() {
		StringBuilder str = new StringBuilder();
		str.append(key).append(",");
		str.append(lockId).append(",");
		str.append(expireTime).append(",");
		str.append(lockSeconds);
		return str.toString();
	}
	
	public static LockNode parseOf(String text) {
		if (text==null||text.isEmpty()) {
			return null;
		}
		String[] values = text.split(",");
		if (values.length!=4) {
			throw new IllegalArgumentException("无效的sv文本格式:"+text);
		}
		
		LockNode sv = new LockNode();
		sv.setKey(values[0]);
		sv.setLockId(values[1]);
		sv.setExpireTime(Long.parseLong(values[2]));
		sv.setLockSeconds(Integer.parseInt(values[3]));
		return sv;
		
	}

}
